﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class ExamSummaryBLL : CommonBLL<ExaminationSummaryInfo, ExamSummaryDAL>
    {
        public static IQueryable<ExaminationSummaryInfo> GetList(int type, string title, string student, int isScoring, int isExcellent = -1)
        {
            var re = GetList();
            re = re.Where(o => o.ExaminationInfo.Type != 2 && o.ExaminationInfo.Type != 3);
            if (type != -1)
                re = re.Where(o => o.ExaminationInfo.Type == type);
            if (!string.IsNullOrEmpty(title))
                re = re.Where(o => o.ExaminationInfo.Title.Contains(title));
            if (!string.IsNullOrEmpty(student))
                re = re.Where(o => o.StudentInfo.StuName.Contains(student));
            if (isScoring != -1)
            {
                re = re.Where(o => o.IsScoring == (isScoring == 0));
            }
            if (isExcellent != -1)
                re = re.Where(o => o.IsExcellent == (isExcellent == 1));
            return re;
        }

        /// <summary>
        /// 如果是考试排除未提交的
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IQueryable<ExaminationSummaryInfo> GetList(int uid, int type)
        {
            var re = GetList();
            re = re.Where(o => o.Uid == uid && o.IsScoring);
            if (type != -1)
                re = re.Where(o => o.ExaminationInfo.Type == type);
            if (type == 4)
                re = re.Where(o => o.IsCommit);
            return re;
        }

        public static int GetTypeCount(int id, int type)
        {
            var re = GetList();
            return re.Count(o => o.Uid == id && o.ExaminationInfo.Type == type);
        }

        public static int GetTypeCount(int type)
        {
            var re = GetList();
            return re.Count(o => o.ExaminationInfo.Type == type);
        }

        public static ExaminationSummaryInfo Get(int id)
        {
            using (var dal = new ExamSummaryDAL())
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static int GetExamScore(int uid)
        {
            using (var dal = new ExamSummaryDAL())
            {
                var re = dal.Select(o => o.ExaminationInfo.Type == 4 && o.StudentInfo.Id == uid);
                if (re.Count == 0)
                    return 0;
                return re.Max(o => o.Score);
            }
        }

        /// <summary>
        /// 学生练习和作业总成绩
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static int GetSumScore(int uid)
        {
            var re = GetList();
            re = re.Where(o => o.StudentInfo.Id == uid && (o.ExaminationInfo.Type == 0 || o.ExaminationInfo.Type == 1));
            return re.Any() ? re.Sum(o => o.Score) : 0;
        }

        public static ExaminationSummaryInfo Get(int uid, int eid)
        {
            using (var dal = new ExamSummaryDAL())
            {
                var re = dal.SelectSingle(o => o.Uid == uid && o.Eid == eid);
                return re;
            }
        }

        public static bool Del(int id)
        {
            using (var dal = new ExamSummaryDAL())
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }

    }
}
